home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / doc / python-nevow / nevow-gettingstarted.txt < prev    next >
Encoding:
Text File  |  2009-03-23  |  3.1 KB  |  79 lines

  1. Getting Started with Nevow
  2. ==========================
  3.  
  4. Warning: This document has only just been started. It's not going to get you
  5. very far right now.
  6.  
  7. Nevow is a reasonably large library and can be quite daunting at first. This
  8. document's aim is to guide the first time user in building a Nevow application.
  9.  
  10.  
  11. Our First Application
  12. ---------------------
  13.  
  14. Let's dive straight in, here's the code for our first (very, very simple)
  15. application. Create the following module, helloworld.py::
  16.  
  17.  
  18.     from nevow import loaders, rend
  19.     
  20.     class HelloWorld(rend.Page):
  21.         addSlash = True
  22.         docFactory = loaders.xmlfile('helloworld.html')
  23.         
  24.  
  25. It looks quite simple but let's walk through it anyway.
  26.  
  27. First, we import two Nevow modules. ``nevow.loaders`` contains template loaders
  28. of which the two most useful are ``xmlfile`` and ``stan``. ``xmlfile`` can load
  29. any well-formed XML (i.e. XHTML) file; ``stan`` loads a stan tree (more on these
  30. later). The other module, ``nevow.rend``, contains all Nevow's standard renders,
  31. many of which we'll meet in this document.
  32.  
  33. We then define the ``HelloWorld`` class that subclasses ``rend.Page``, Nevow's
  34. main resource class. ``HelloWorld`` has two class attributes. ``addSlash`` tells
  35. rend.Page to redirect to a version of the request URL that ends in a '/' if
  36. necessary. You generally want to set this to ``True`` for the root resource.
  37. ``docFactory`` tells the page instance where to get the template from. In this
  38. case we're providing a loader that parses an HTML file (not shown) from disk.
  39.  
  40. Hmm, ok I hear you say but how do I see it. Well, Twisted provides a good web
  41. server which we can use. Twisted also includes a clever little application for
  42. starting Twisted applications. Here's the helloworld.tac file, a Twisted Application
  43. Configuration::
  44.  
  45.     from twisted.application import internet
  46.     from twisted.application import service
  47.     from nevow import appserver
  48.     import helloworld
  49.     
  50.     application = service.Application('helloworld')
  51.     site = appserver.NevowSite(helloworld.HelloWorld())
  52.     webServer = internet.TCPServer(8080, site)
  53.     webServer.setServiceParent(application)
  54.  
  55.  
  56. Give it a go, run the following and connect to http://localhost:8080/ to see
  57. your application::
  58.  
  59.     twistd -noy helloworld.tac
  60.     
  61.     
  62. You'll probably notice that you get log output on the console. This is just one
  63. of the good things that twistd does. It can also daemonize the application, shed
  64. privileges if run as root, etc.
  65.  
  66. TAC files are covered in more detail in the Twisted documentation but let's
  67. quickly explain what all this does anyway.
  68.  
  69. When twistd starts up it loads the .tac file (it's just Python) and looks for
  70. the attribute called ``application``. When twistd is all ready to go it starts
  71. the ``application``.
  72.  
  73. The application is not much use unless it actually does something so the next
  74. thing we do is create a NevowSite instance, ``site``, and pass it a root
  75. resource, a ``HelloWorld`` instance. Finally, we create a TCP server that makes
  76. the site available on port 8080 and bind the server to the application to ensure
  77. the server is started when the application is started.
  78.  
  79.